<?php

/**
 * This is the model class for table "ham_user".
 *
 * The followings are the available columns in table 'ham_user':
 * @property integer $id
 * @property string $username
 * @property string $email
 * @property string $password
 * @property integer $status
 * @property integer $group_id
 * @property string $created_at
 * @property string $updated_at
 *
 * The followings are the available model relations:
 * @property Account[] $accounts
 * @property AccountInformation[] $accountInformations
 * @property Campain[] $campains
 * @property Contact[] $contacts
 * @property ContactCustomField[] $contactCustomFields
 * @property Group[] $groups
 * @property Template[] $templates
 * @property UserGroup $group
 * @property UserLog $userLog
 * @property UserProfile[] $userProfiles
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ham_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, email, password', 'required'),
			array('status, group_id', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>200),
			array('email', 'length', 'max'=>255),
			array('password', 'length', 'max'=>128),
			array('created_at, updated_at', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, email, password, status, group_id, created_at, updated_at', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'accounts' => array(self::HAS_MANY, 'Account', 'user_id'),
			'accountInformations' => array(self::HAS_MANY, 'AccountInformation', 'user_id'),
			'campains' => array(self::HAS_MANY, 'Campain', 'created_by'),
			'contacts' => array(self::HAS_MANY, 'Contact', 'created_by'),
			'contactCustomFields' => array(self::HAS_MANY, 'ContactCustomField', 'created_by'),
			'groups' => array(self::HAS_MANY, 'Group', 'created_by'),
			'templates' => array(self::HAS_MANY, 'Template', 'created_by'),
			'group' => array(self::BELONGS_TO, 'UserGroup', 'group_id'),
			'userLog' => array(self::HAS_ONE, 'UserLog', 'id'),
			'userProfile' => array(self::HAS_ONE, 'Profile', 'user_id'),
		);
	}
        
        public function getAvatar() {
//            $profile = $this->userProfile;
            $profile = UserProfile::model()->find('user_id = :user_id', array(':user_id' => $this->id));
            $href = yii::app()->createUrl('');
            $str = $href . '/img/avatar.jpg';
            if(is_object($profile) && file_exists(yii::app()->basePath . '/../upload/'. $profile->avatar)) {
                $str = $href . '/upload/' . $profile->avatar;
            }
            return $str;
        }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'email' => 'Email',
			'password' => 'Password',
			'status' => 'Status',
			'group_id' => 'Group',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}